import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import * as path from 'path';
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { viteMockServe } from 'vite-plugin-mock'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  return {
    define: {
      'process.env': process.env, // solve the process not define problem
      'process.platform': null,
      'process.version': null
    },
    resolve: {
      // 设置别名
      alias: {
        '@': path.resolve(__dirname,'src'),
      },
    },
    plugins: [
      vue(),
      // 自动引入饿了吗css
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      // mocks
      viteMockServe({
        ignore: /^\_/,
        mockPath: './src/mock', // mock文件地址
        localEnabled: loadEnv(mode, process.cwd()).VITE_MOCK === 'true', // 开发打包开关
        prodEnabled: command !== 'serve', // 生产打包开关
        supportTs: true, // 打开后，可以读取 ts 文件模块。 请注意，打开后将无法监视.js 文件
        injectCode: `
                import { setupProdMockServer } from './mockServer';
                setupProdMockServer();
              `
      }),
      // svg
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',

        /**
         * 自定义插入位置
         * @default: body-last
         */
        // inject?: 'body-last' | 'body-first'

        /**
         * custom dom id
         * @default: __svg__icons__dom__
         */
        // customDomId: '__svg__icons__dom__',
      }),
    ],
    server: {
      port: 5064, // 启动端口
      hmr: {
        host: '127.0.0.1',
        port: 5064,
      },
      // 设置 https 代理
      proxy: {
        '/bkapi': {
          target: 'http://127.0.0.1:3000',
          changeOrigin: true,
          rewrite: (path: string) => { return path.replace(/^\/bkapi/, '') }
        },
        '/socket.io': {
          target: 'http://127.0.0.1:3001',
          changeOrigin: true,
          rewrite: (path: string) => { return path.replace(/^\/socket.io/, '') }
        },
      }
    },
  }
})
